package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.task.PinglunzhaopianTask;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.PublishCommentTask;

import java.net.URLDecoder;


public class PublishCommentActivity extends BaseActivity implements OnClickListener{
	private static final String TAG = "PublishCommentActivity";
	private Context mContext;
	private TextView cancleText;
	private TextView submitText;
	private EditText commentEdit;
	private String commentString;
	private int commentTaskId = -1;
	private int zcommentTaskId = -1;
	private String videoId;
	private String targetId;
	//private boolean type;
	private String nickname;
	private int type;
	private static final int RESULT_CODE = 100;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.publishcomment);
		mContext = this;
		initView();
	}
	
	private void initView(){
		Intent intent = getIntent();
		if(intent != null){
			videoId = intent.getStringExtra("VideoId");
			targetId = intent.getStringExtra("UserId");
			nickname = intent.getStringExtra("NickName");
			type = intent.getIntExtra("type",0);
		}
		cancleText = (TextView)findViewById(R.id.cancle_btn);
		submitText = (TextView)findViewById(R.id.submit_btn);
		commentEdit = (EditText)findViewById(R.id.comment_edit);
		cancleText.setOnClickListener(this);
		submitText.setOnClickListener(this);
			if(nickname != null){
				commentEdit.setText(getString(R.string.reback_to, URLDecoder.decode(nickname)));
				commentEdit.setSelection(commentEdit.getText().length());
			}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit_btn:
			commentString = commentEdit.getText().toString();
			if(!TextUtils.isEmpty(commentString)){
				postComment();
			}else{
				Toast.makeText(mContext, "评论内容不能为空", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.cancle_btn:
			finish();
			break;

		default:
			break;
		}
		
	}
	
	private void postComment(){
		if(commentTaskId != -1 || zcommentTaskId !=-1){
			return;
		}
		if (type ==1) {
			PublishCommentTask task = new PublishCommentTask(videoId, commentString, targetId, "");
			task.addListener(new CommentListener(mContext));
			commentTaskId = getEatShowApp().getTaskManager().addTask(task);
		}else if (type ==2){
			PinglunzhaopianTask ztask = new PinglunzhaopianTask(commentString,videoId,targetId);
			ztask.addListener(new CommentListener(mContext));
			zcommentTaskId = getEatShowApp().getTaskManager().addTask(ztask);
		}

	}

	class CommentListener extends ShowToastTaskEventListener {

		public CommentListener(Context context) {
			super(context);
		}

		@Override
		public void handleTaskError(int taskId, int errorCode, String errorMsg) {
			if (taskId == commentTaskId) {
				commentTaskId = -1;
			}else if (taskId == zcommentTaskId){
				zcommentTaskId = -1;
			}
			super.handleTaskError(taskId, errorCode, errorMsg);
		}

		@Override
		public void handleTaskCompleted(int taskId, int result, Object data) {
			if (taskId == commentTaskId) {
				commentTaskId = -1;
			}else if (taskId == zcommentTaskId){
				setResult(RESULT_CODE);
				zcommentTaskId =-1;
			}
			finish();

			super.handleTaskCompleted(taskId, result, data);
		}



	}

}
